import crypto from '../utils/crypto';
import * as express from 'express';
import db from '../utils/db';
import http from '../utils/http';

//获取类型声明//
import configs_win from '../configs_win';
const ACCOUNT_TYPE = configs_win.account_server();
type account_config = typeof ACCOUNT_TYPE;
//获取类型声明//



const app = express();
let halladdr = '';
let config: account_config = null;




function send(res, ret){
    let str = JSON.stringify(ret);
    res.send(str);
}


/**设置跨域访问 */
app.all('*', function(req, res, next){
    res.header('Access-Control-Allow-Origin', '*');
    res.header('Access-Control-Allow-Headers', 'X-Requested-With');
    res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS');
    res.header('X-Powered-By', '3.2.1');
    res.header('Content-Type', 'application/json;charset=utf-8');
    next();
});


//返回服务器版本号跟下载地址
app.get('/get_serverinfo', function(req, res){
    let ret = {
        version: config.VERSION,
        hall: halladdr,
        appweb: config.APP_WEB
    };
    send(res, ret);
});



app.get('/download', function(req, res){
    res.send('这里是游戏下载页面');
});

//游客登陆
app.get('/guest', function(req, res){
    const account = 'guest_' + req.query.account;
    const sign = crypto.md5(account + req.ip + config.ACCOUNT_PRI_KEY);
    const ret: ret = {
        errcode:0,
        errmsg: 'ok',
        account,
        halladdr,
        sign
    };
    send(res, ret);
});


export default class as{

    /**
     * 开启账号服务器
     */
    static start(cfg:account_config){
        config = cfg;
        halladdr = config.HALL_IP + ':' + config.HALL_CLIENT_PORT;
        app.listen(config.CLIENT_PORT);
        console.log('账号服务器运行在：' + config.CLIENT_PORT + '端口.');
    }

}